<?php
defined('_JEXEC') or die();

jimport('joomla.application.component.controller');

require_once(JPATH_COMPONENT.DS.'helpers'.DS.'extensionshelper.php');

class Easiv2ControllerCheckCommunication extends JController {
	
	var $db;
	
	function __construct() {
		$this->db = JFactory::getDBO();
		
		parent::__construct();
	}
	
	/**
	 * This is a simple method to check, if the management instance can communicate with this instance
	 * 
	 * @return true
	 */
	function isInstanceResponding() {
		echo "true";
	}
	
	function setIdentifier() {
		$param = xmlrpc_decode($_POST['ids']);

		$current_identifier = $param[0];
		$new_identifier = $param[1];
		
		$this->db->setQuery("SELECT identifier FROM #__easiv2_mi");
		$result = $this->db->loadObject();
		
		if(count($result) == 0) {
			$this->db->setQuery("INSERT INTO #__easiv2_mi VALUES (null, '".$new_identifier."')");
			$this->db->query();
			
			echo "identifierSet: ".$new_identifier;
		}
		else {
			if($result->identifier == $current_identifier) {
				$this->db->setQuery("UPDATE #__easiv2_mi SET identifier = '".$new_identifier."'");
				$this->db->query();
				
				echo "identifierUpdated";
			}
			else {
				echo "identifierWrong";
			}
		}
	}
}